home *** CD-ROM | disk | FTP | other *** search
/ Amiga CD-Sensation: Golden Games / Amiga CD-Sensation - Ausgabe 2 - Golden Games (1996)(GTI - Schatztruhe)(DE)[!].iso / Adventurer's / ImpPro / Developer / BGUIModule.c next >
C/C++ Source or Header  |  1995-06-05  |  13KB  |  553 lines

  1. /**************************************************************************
  2.  *                                                                        *
  3.  * Dice Module --- A dice-rolling module for Imp Professional             *
  4.  *                                                                        *
  5.  **************************************************************************/
  6.  
  7. #include <datatypes/arexxclass.h>
  8. #include <libraries/bgui.h>
  9. #include <libraries/bgui_macros.h>
  10. #include <libraries/gadtools.h>
  11.  
  12. #include <clib/alib_protos.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/bgui.h>
  17. #include <proto/intuition.h>
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22.  
  23. #include "Module.h"
  24.  
  25. // ID's for BGUI gadgets
  26.  
  27. #define ID_txtCOMBO    100L
  28. #define ID_cmdROLL    101L
  29. #define ID_cmdCLEAR    102L
  30. #define ID_lstDice    103L
  31. #define ID_cmdAdd    104L
  32. #define ID_cmdDel    105L
  33.  
  34. // Menu strip
  35.  
  36. #define ID_LOAD        1001L
  37. #define ID_SAVE        1002L
  38. #define ID_ABOUT        1003L
  39. #define ID_QUIT        1004L
  40. #define ID_SNAPSHOT        1005L
  41. #define ID_AREXX        1006L
  42.  
  43. struct NewMenu MainMenus[] = {
  44.     Title( "Project" ),
  45.         Item( "Load die strip...", "L", ID_LOAD ),
  46.         Item( "Save die strip...", "S", ID_SAVE ),
  47.         ItemBar,
  48.         Item( "AREXX launch...", "A", ID_AREXX ),
  49.         ItemBar,
  50.          Item( "About...", "?", ID_ABOUT ),
  51.         Item( "Quit",     "Q", ID_QUIT  ),
  52.     Title( "Settings" ),
  53.         Item( "Snapshot window", "N", ID_SNAPSHOT ),
  54.     End
  55. };
  56.  
  57. // Globals
  58.  
  59. struct Library            *BGUIBase = NULL;
  60. struct Library            *ImpBase = NULL;
  61. struct ImpSnapshot         ssMain;
  62. ULONG                 Total = 0, Result = 0;
  63. struct Window            *window;
  64. Class                *ARexxClass;
  65. Object                *WO_Window, *GO_lstDice, *GO_Combo, *GO_Result, *GO_Total, *AO_Rexx,
  66.                     *GO_Roll, *GO_Clear, *GO_Add, *GO_Del;
  67. BOOL                     running = TRUE, Blind = FALSE;
  68. UBYTE                 rxbuf[256];
  69.  
  70. struct EasyStruct errEasyStruct =               // The error requester
  71. {
  72.    sizeof(struct EasyStruct), 0, "Dice Module ERROR",
  73.    NULL, "Give up!"
  74. };
  75.  
  76. REXXCOMMAND Commands[] = {
  77.     "RAND",        "/N",                    rx_Rand,
  78.     "ROLL",        "/A",                    rx_Roll,
  79.     "CLEAR",        NULL,                    rx_Clear,
  80.     "GET",        "COMBO/S, TOTAL/S",            rx_Get,
  81.     "BLIND",        "ON/S, OFF/S",                rx_Blind,
  82.     "QUIT",        NULL,                    rx_Quit,
  83. };
  84.  
  85. // Aux functions
  86.  
  87. UBYTE    *AboutText    =    ISEQ_C "Dice module for Imp Professional\n\n"
  88.                         "Version " MOD_VERSION ", compiled " __DATE__;
  89.  
  90. void Sprintf(char *buffer, const char *ctl, ...)
  91. {
  92.    va_list args;
  93.  
  94.    va_start(args, ctl);
  95.    RawDoFmt(ctl, args, (void (*))"\x16\xc0\x4e\x75", buffer);
  96.    va_end(args);
  97. }
  98.  
  99. /*
  100. **      Put up a simple requester.
  101. **/
  102. ULONG Req( struct Window *win, UBYTE *gadgets, UBYTE *body, ... )
  103. {
  104.         struct bguiRequest      req = { NULL };
  105.  
  106.         req.br_GadgetFormat     = gadgets;
  107.         req.br_TextFormat       = body;
  108.         req.br_Flags            = BREQF_CENTERWINDOW|BREQF_XEN_BUTTONS;
  109.  
  110.         return( BGUI_RequestA( win, &req, ( ULONG * )( &body + 1 )));
  111. }
  112.  
  113. void Toggle_gadgets(BOOL value)
  114. {
  115.     SetGadgetAttrs((struct Gadget *)GO_Del, window, NULL, GA_Disabled, value, TAG_END);
  116. }
  117.  
  118. int LoadRequest(char *ret)
  119. {
  120.     Object    *filereq;
  121.      ULONG    pa, rc;
  122.  
  123.     filereq = FileReqObject, ASLFR_DoPatterns, TRUE,
  124.                         ASLFR_TitleText, (ULONG) "Select die strip to load",
  125.                         ASLFR_InitialDrawer, (LONG) SAVE_DIRECTORY,
  126.                         ASLFR_InitialPattern, "#?.strip",
  127.                         ASLFR_Window, window,
  128.                         EndObject;
  129.     if ( filereq ) {
  130.         if ( ! ( rc = DoRequest( filereq )))
  131.         {
  132.                               GetAttr( FRQ_Path, filereq, &pa);
  133.                         strcpy(ret, (char *)pa);
  134.                         DisposeObject(filereq);
  135.                         return TRUE;
  136.         }
  137.         DisposeObject( filereq);
  138.     } else
  139.         Do_requester( "Unable to create filerequester object." );
  140.     return FALSE;
  141. }
  142.  
  143. int SaveRequest(char *ret)
  144. {
  145.     Object    *filereq;
  146.      ULONG    pa, rc;
  147.  
  148.     filereq = FileReqObject, ASLFR_DoPatterns, TRUE,
  149.                         ASLFR_TitleText, (ULONG) "Select die strip to save",
  150.                         ASLFR_InitialDrawer, (LONG) SAVE_DIRECTORY,
  151.                         ASLFR_InitialPattern, "#?.strip",
  152.                         ASLFR_Window, window,
  153.                         ASLFR_InitialFile, "unnamed.strip",
  154.                         ASLFR_DoSaveMode, TRUE,
  155.                         EndObject;
  156.     if ( filereq ) {
  157.         if ( ! ( rc = DoRequest( filereq )))
  158.         {
  159.                               GetAttr( FRQ_Path, filereq, &pa);
  160.                         strcpy(ret, (char *)pa);
  161.                         DisposeObject(filereq);
  162.                         return TRUE;
  163.         }
  164.         DisposeObject( filereq);
  165.     } else
  166.         Do_requester( "Unable to create filerequester object." );
  167.     return FALSE;
  168. }
  169.  
  170. void Display_die(int n)
  171. {
  172.     Total += n;
  173.     Result = n;
  174.  
  175.     SetGadgetAttrs((struct Gadget *)GO_Result, window, NULL, INFO_Args, &Result, TAG_END);
  176.     SetGadgetAttrs((struct Gadget *)GO_Total, window, NULL, INFO_Args, &Total, TAG_END);
  177. }
  178.  
  179. void cmdClearClicked( void )
  180. {
  181.     Total = 0;
  182.     Result = 0;
  183.  
  184.     SetGadgetAttrs((struct Gadget *)GO_Total, window, NULL, INFO_Args, &Total, TAG_END);
  185.     SetGadgetAttrs((struct Gadget *)GO_Result, window, NULL, INFO_Args, &Result, TAG_END);
  186. }
  187.  
  188. void cmdRollClicked( void )
  189. {
  190.     ULONG temp;
  191.     char buf[BUF];
  192.  
  193.     GetAttr(STRINGA_TextVal, GO_Combo, &temp);
  194.     SetGadgetAttrs((struct Gadget *)GO_Combo, window, NULL,
  195.                 STRINGA_BufferPos, 0, TAG_END);
  196.     strcpy(buf, (UBYTE *)temp);
  197.     Display_die(impInterpretAndRoll( buf ));
  198. }
  199.  
  200. void txtComboClicked( void )
  201. {
  202.     cmdRollClicked();
  203.     ActivateGadget((struct Gadget *)GO_Combo, window, NULL);
  204. }
  205.  
  206. void Get_buttons(UBYTE *fname)
  207. {
  208.      BPTR fh;
  209.      UBYTE buf[BUF], *temp;
  210.     UBYTE filename[108];
  211.  
  212.     if (fname == NULL)
  213.     {
  214.         if (!LoadRequest(filename))
  215.             return;
  216.     }
  217.     else
  218.     {
  219.         strcpy(filename, fname);
  220.     }
  221.  
  222.      if (fh = Open(filename, MODE_OLDFILE))
  223.      {
  224.         LockList(GO_lstDice);
  225.         ClearList(window, GO_lstDice);
  226.          while (FGets(fh, buf, BUF))
  227.          {
  228.             temp = strchr(buf, '\n');     // Strip the trailing newline
  229.             *temp = '\0';
  230.             AddEntry(window, GO_lstDice, buf, LVAP_TAIL);
  231.         }
  232.         UnlockList(window, GO_lstDice);
  233.         Close( fh );
  234.     }
  235. }
  236.  
  237. void Put_buttons( void )
  238. {
  239.     APTR worknode;
  240.     char filename[BUF];
  241.     BPTR fh;
  242.  
  243.     if (SaveRequest(filename))
  244.     {
  245.         if (fh = Open(filename, MODE_NEWFILE))
  246.         {
  247.             if ( worknode = (APTR)DoMethod(GO_lstDice, LVM_FIRSTENTRY, NULL, 0L))
  248.             {
  249.                 do {
  250.                     FPuts(fh, worknode);
  251.                     FPutC(fh, '\n');
  252.                     worknode = (APTR)DoMethod(GO_lstDice, LVM_NEXTENTRY, worknode, 0L);
  253.                 } while (worknode);
  254.             }
  255.             Close(fh);
  256.         }
  257.     }
  258. }
  259.  
  260. void cmdAddClicked(void)
  261. {
  262.     ULONG tmp;
  263.  
  264.     GetAttr(STRINGA_TextVal, GO_Combo, &tmp);
  265.  
  266.     if (strcmp((UBYTE *)tmp, ""))
  267.     {
  268.         LockList(GO_lstDice);
  269.         AddEntrySelect(window, GO_lstDice, (UBYTE *)tmp, LVAP_TAIL);
  270.         UnlockList(window, GO_lstDice);
  271.     }
  272. }
  273.  
  274. void cmdDelClicked(void)
  275. {
  276.     ULONG obj;
  277.  
  278.     obj = FirstSelected(GO_lstDice);
  279.  
  280.     if (obj)
  281.     {
  282.         LockList(GO_lstDice);
  283.         RemoveEntryVisible(window, GO_lstDice, obj);
  284.         UnlockList(window, GO_lstDice);
  285.         Toggle_gadgets(TRUE);
  286.     }
  287. }
  288.  
  289. void lstDiceClicked(void)
  290. {
  291.     static ULONG ds[2], dm[2], last = 0, clicked;
  292.     UBYTE *diestring;
  293.  
  294.     GetAttr( LISTV_LastClicked, GO_lstDice, &clicked );
  295.  
  296.     if ( clicked == last ) {
  297.         CurrentTime( &ds[ 1 ], &dm[ 1 ] );
  298.             if ( DoubleClick( ds[ 0 ], dm[ 0 ], ds[ 1 ], dm [ 1 ] )) {
  299.                 /* Double clicked */
  300.                 txtComboClicked();
  301.                 last = NULL;
  302.             }
  303.             else
  304.                 last = NULL;
  305.     }
  306.     else
  307.     {
  308.         CurrentTime( &ds[ 0 ], &dm[ 0 ] );
  309.         last = clicked;
  310.         ActivateGadget((struct Gadget *)GO_Combo, window, NULL);
  311.     }
  312.  
  313.     diestring = (UBYTE *)clicked;
  314.  
  315.     SetGadgetAttrs((struct Gadget *)GO_Combo, window, NULL, STRINGA_TextVal, diestring, TAG_END);
  316.     Toggle_gadgets(FALSE);
  317. }
  318.  
  319. void Do_requester( STRPTR errmsg )
  320. {
  321.     struct EasyStruct errEZ;
  322.  
  323.     if (errmsg)
  324.     {
  325.         errEZ = errEasyStruct;           // Display error message passed in
  326.         errEZ.es_TextFormat = errmsg;
  327.         EasyRequest(NULL, &errEZ, NULL);
  328.     }
  329. }
  330.  
  331. int main(int argc,char *argv[])
  332. {
  333.     ULONG ret, signal, rc, tmp, rxsig = NULL;
  334.  
  335.     if ( ImpBase = OpenLibrary( IMPLIBNAME, IMPLIBVERSION )) {
  336.       if ( BGUIBase = OpenLibrary( BGUINAME, BGUIVERSION )) {
  337.        if ( ARexxClass = InitARexxClass() ) {
  338.         AO_Rexx = NewObject( ARexxClass, NULL, AC_HostName, "IMPDICE", AC_CommandList, Commands, TAG_END );        
  339.           /*
  340.           **      Create the window object.
  341.           **/
  342.         WO_Window = WindowObject,
  343.             WINDOW_Title,            "Dice",
  344.             WINDOW_SizeGadget,        TRUE,
  345.             WINDOW_ScaleWidth,      30,
  346.             WINDOW_MenuStrip,       MainMenus,
  347.             WINDOW_HelpFile,        "ImpPro:ImpPro.Guide",
  348.             WINDOW_HelpNode,        "Dice_module",
  349.             WINDOW_SmartRefresh,    TRUE,
  350.             WINDOW_PubScreenName,    "IMP.SCREEN",
  351.             WINDOW_MasterGroup,
  352.                 HGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ), FillRaster,
  353.                     StartMember,
  354.                         VGroupObject, Spacing( 1 ),
  355.                             StartMember,
  356.                                 GO_lstDice = StrListview(NULL, NULL, ID_lstDice),
  357.                             EndMember,
  358.                             StartMember,
  359.                                 HGroupObject, Spacing( 1 ),
  360.                                     StartMember,
  361.                                         GO_Add = XenKeyButton("_Add", ID_cmdAdd),
  362.                                     EndMember,
  363.                                     StartMember,
  364.                                         GO_Del = XenKeyButton("_Del", ID_cmdDel),
  365.                                     EndMember,
  366.                                 EndObject, FixMinHeight,
  367.                             EndMember,
  368.                         EndObject,
  369.                     EndMember,
  370.                     StartMember,
  371.                         VGroupObject, Spacing( 2 ),
  372.                             StartMember, GO_Roll = XenKeyButton("_Roll", ID_cmdROLL), Weight(110), EndMember,
  373.                             StartMember, GO_Clear = XenKeyButton("_Clear", ID_cmdCLEAR), EndMember,
  374.                         EndObject, FixMinWidth,
  375.                     EndMember,
  376.                     StartMember,
  377.                         VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ), Weight(110),
  378.                             VarSpace(20),
  379.                             StartMember, GO_Combo  = KeyString("C_ombo", "", 20, ID_txtCOMBO), EndMember, FixMinHeight,
  380.                             VarSpace(20),
  381.                             StartMember, HorizSeperator, EndMember,
  382.                             VarSpace(20),
  383.                             StartMember, GO_Result = InfoFixed("Result", "\33b\33c%ld", &Result, 1), EndMember, FixMinHeight,
  384.                             StartMember, GO_Total  = InfoFixed("Total", "\33c%ld", &Total, 1), EndMember, FixMinHeight,
  385.                             VarSpace(20),
  386.                         EndObject, Weight(150),
  387.                     EndMember,
  388.                 EndObject,
  389.         EndObject;
  390.  
  391.         ssMain.is_ID = MAINWIN_ID;
  392.         if (impReadSnapshot(&ssMain))
  393.             SetAttrs(WO_Window, WINDOW_Bounds, &ssMain.is_Bounds, TAG_END);
  394.  
  395.         if (WO_Window ) {
  396.             tmp  = GadgetKey( WO_Window, GO_Roll,  "r" );
  397.             tmp += GadgetKey( WO_Window, GO_Clear, "c" );
  398.             tmp += GadgetKey( WO_Window, GO_Combo, "o" );
  399.             if ( tmp == 3) {
  400.                 if ( window = WindowOpen( WO_Window )) {
  401.                     GetAttr( WINDOW_SigMask, WO_Window, &signal );
  402.                     GetAttr( AC_RexxPortMask, AO_Rexx, &rxsig );
  403.                     Get_buttons(BUTTON_FILENAME);
  404.                     impSeedRand(NULL);
  405.                     Toggle_gadgets(TRUE);
  406.                     ActivateGadget((struct Gadget *)GO_Combo, window, NULL);
  407.                     do {
  408.                         ret = Wait( signal | rxsig );
  409.                           if (ret & rxsig )
  410.                             DoMethod( AO_Rexx, ACM_HANDLE_EVENT );
  411.  
  412.                         if (ret & signal)
  413.                         {
  414.                             while (( rc = HandleEvent( WO_Window )) != WMHI_NOMORE ) {
  415.                                 switch ( rc ) {
  416.                                     case    WMHI_CLOSEWINDOW:
  417.                                     case    ID_QUIT:
  418.                                         running = FALSE;
  419.                                         break;
  420.                                     case ID_ABOUT:
  421.                                         Req(window, "Okay", AboutText);
  422.                                         break;
  423.                                     case ID_LOAD:
  424.                                         Get_buttons(NULL);
  425.                                         break;
  426.                                     case ID_SAVE:
  427.                                         Put_buttons();
  428.                                         break;
  429.                                     case ID_txtCOMBO:
  430.                                         txtComboClicked();
  431.                                         break;
  432.                                     case ID_cmdROLL:
  433.                                         cmdRollClicked();
  434.                                         break;
  435.                                     case ID_cmdCLEAR:
  436.                                         cmdClearClicked();
  437.                                         break;
  438.                                     case ID_lstDice:
  439.                                         lstDiceClicked();
  440.                                         break;
  441.                                     case ID_cmdAdd:
  442.                                         cmdAddClicked();
  443.                                         break;
  444.                                     case ID_cmdDel:
  445.                                         cmdDelClicked();
  446.                                         break;
  447.                                     case ID_SNAPSHOT:
  448.                                         GetAttr(WINDOW_Bounds, WO_Window, (ULONG *)&ssMain.is_Bounds);
  449.                                         impWriteSnapshot(&ssMain);
  450.                                         break;
  451.                                     case ID_AREXX:
  452.                                         impARexxSelector("dice");
  453.                                         break;
  454.                                 }
  455.                             }
  456.                         }
  457.                     } while ( running );
  458.                 } else
  459.                     Do_requester ( "Could not open the window" );
  460.             } else
  461.                 Do_requester ( "Could not assign gadget keys" );
  462.             if (WO_Window)        DisposeObject( WO_Window );
  463.             if (AO_Rexx)        DisposeObject( AO_Rexx );
  464.         } else
  465.             Do_requester ( "Could not create the window object" );
  466.          FreeARexxClass( ARexxClass );
  467.         } else
  468.             Do_requester ( "Could not initialize AREXX Class" );
  469.         CloseLibrary( BGUIBase );
  470.       } else
  471.           Do_requester ( "Unable to open the bgui.library" );
  472.     CloseLibrary( ImpBase );
  473.     } else
  474.         Do_requester ( "Unable to open " IMPLIBNAME );
  475.     return( 0 );
  476. }
  477.  
  478. VOID rx_Quit( REXXARGS *ra, struct RexxMsg *rxm )
  479. {
  480.     running = FALSE;
  481.     ra->ra_Result = "OK";
  482. }
  483.  
  484. VOID rx_Clear( REXXARGS *ra, struct RexxMsg *rxm )
  485. {
  486.     cmdClearClicked();
  487.     ra->ra_Result = "OK";
  488. }
  489.  
  490. VOID rx_Roll( REXXARGS *ra, struct RexxMsg *rxm )
  491. {
  492.     ULONG ret;
  493.  
  494.     if (ra->ra_ArgList[0])
  495.     {
  496.         if (Blind)
  497.             ret = impInterpretAndRoll((STRPTR)ra->ra_ArgList[0]);
  498.         else
  499.         {
  500.             SetGadgetAttrs((struct Gadget *)GO_Combo, window, NULL,
  501.                         STRINGA_TextVal, (UBYTE *)ra->ra_ArgList[0], TAG_END);
  502.             cmdRollClicked();
  503.             ret = Result;
  504.         }
  505.     }
  506.     else
  507.     {
  508.         cmdRollClicked();
  509.         ret = Result;
  510.     }
  511.     Sprintf(rxbuf, "%ld", ret);
  512.     ra->ra_Result = rxbuf;
  513. }
  514.  
  515. VOID rx_Rand( REXXARGS *ra, struct RexxMsg *rxm )
  516. {
  517.     LONG ret;
  518.  
  519.     if (ra->ra_ArgList[0])
  520.     {
  521.         ret = impRand(*((ULONG *)ra->ra_ArgList[0]));
  522.         Sprintf(rxbuf, "%ld", ret);
  523.         ra->ra_Result = rxbuf;
  524.     }
  525.     else
  526.         ra->ra_Result = "Missing parameter!";
  527. }
  528.  
  529. VOID rx_Get( REXXARGS *ra, struct RexxMsg *rxm )
  530. {
  531.     ULONG ret;
  532.  
  533.     if (ra->ra_ArgList[0])
  534.     {
  535.         GetAttr(STRINGA_TextVal, GO_Combo, &ret);
  536.         strcpy(rxbuf, (UBYTE *)ret);
  537.     }
  538.     if (ra->ra_ArgList[1])
  539.         Sprintf(rxbuf, "%ld", Total);
  540.  
  541.     ra->ra_Result = rxbuf;
  542. }
  543.  
  544. VOID rx_Blind( REXXARGS *ra, struct RexxMsg *rxm )
  545. {
  546.     if (ra->ra_ArgList[0])
  547.         Blind = TRUE;    
  548.     else
  549.         Blind = FALSE;
  550.  
  551.     ra->ra_Result = "OK";
  552. }
  553.